package panda.cardutil.other;

import panda.client.engine.MagicCard;
import panda.client.engine.MagicCardDB;
import panda.client.enumerations.MagicSet;

import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Set;
import java.util.HashSet;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.io.File;
import java.io.IOException;

/**
 * Scans a blog post text file and replaces all instances of any cards from listed magic sets with an autocard.php link
 * from magiccards.info. Here is an example:
 *
 *
<div class="decklist">
<div class="type">creatures: 15</div>
2 akrasan squire
1 aven squire
1 steward of valeron
2 naya battlemage
1 gluttonous slime
1 dragonsoul knight
1 wall of reverence
1 edgar's apprentice
2 bloodpyre elemental
3 bull ceradon
<div class="type">spells: 8</div>
1 magma spray
2 sigil blessing
1 naya charm
1 oblivion ring
1 resounding silence
2 gleam of resistance
<div class="type">lands: 17</div>
8 plains
5 forest
1 mountain
1 naya panorama
2 rupture spire
<div class="type">relevant sideboard</div>
1 filigree fracture
1 sigil blessing
1 might of alara
</div>
 
 * <p/>
 * User: kyruru
 * Date: Mar 28, 2009
 * Time: 10:50:52 PM
 */
public class BlogAutocardLinker
{
    public static void main(String[] args) throws IOException
    {
        List<MagicCard> cards = new ArrayList<MagicCard>();

        cards.addAll(MagicCardDB.getCardsFromSet(MagicSet.SHARDS_OF_ALARA));
        cards.addAll(MagicCardDB.getCardsFromSet(MagicSet.CONFLUX));

        String blogcopy = "src/panda/cardutil/other/blogpost.txt";

        Scanner scanner = new Scanner(new File(blogcopy));
        StringBuilder builder = new StringBuilder();

        /* dump the entire blog text file into a StringBuilder */
        while (scanner.hasNextLine())
        {
            builder.append(scanner.nextLine() + "\n");
        }

        /* for each card in shards of alara and conflux, replace all occurances of its name with an autocard link! */
        String linkPrefix = "http://magiccards.info/autocard.php?card=";
        List<String> nameSet = new ArrayList<String>();
        for (MagicCard card : cards)
        {
            String name = card.getName();

            /* prevent acting multiple times on duplicate names, such as basic lands. otherwise we get strings like: */
            /* http://magiccards.info/autocard.php?card=http://magiccards.info/autocard.php?card=http://magiccards.info/autocard.php?card=http://magiccards.info/autocard.php?card=Island */
            if (!nameSet.contains(name))
            {
                nameSet.add(name);

                Pattern namePattern = Pattern.compile(name, Pattern.CASE_INSENSITIVE);
                Matcher nameMatcher = namePattern.matcher(builder);

                builder = new StringBuilder(nameMatcher.replaceAll("<a href=\"" + linkPrefix + name + "\">" + name + "</a>"));
            }
        }

        System.out.println(builder);
    }
}